/**
 * kl yy-ds @2021
 */
package com.cxps.server.thread.service;

import com.cxps.cloud.utils.IdUtils;
import com.cxps.server.product.domain.ProductEntity;
import com.cxps.server.thread.mapper.ThreadProductMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO(类描述)
 *
 * @author YouCai.Liu
 * @since 2021/11/30
 * ...
 */
@Slf4j
public class ThreadService extends Thread {

    private ThreadProductMapper threadProductMapper;
    private int threadSize;
    private int increment = 0;
    private boolean flag = false;

    @Override
    public void run() {

        String name = Thread.currentThread().getName();
        if("myThread5".equals(name)){
            this.flag = true;
        }

        if(this.flag){
            throw new RuntimeException(name + "异常了。。。。");
        }

        List<ProductEntity> list = new ArrayList<>();
        for (int i = 0; i < threadSize; i++) {
            increment++;
            ProductEntity entity = new ProductEntity();
            entity.setId(IdUtils.generateId());
            entity.setProductName("这是单线程插入的商品数据" + increment);
            entity.setProductTitle("这是单线程插入的商品标题数据" + increment);
            entity.setProductCode(IdUtils.generateId());
            entity.setProductType(IdUtils.generateId());
            entity.setChannel(increment);
            entity.setSaleType(increment);
            entity.setUnit("包斤袋");
            entity.setProductImg(IdUtils.generateId() + ".png");
            entity.setProductPrice(Double.parseDouble(increment + ""));
            entity.setSaleNum(increment);
            entity.preInsert();
            entity.setIsUp(1);
            if (list.size() < 500) {
                list.add(entity);
            }
            if (list.size() == 500) {
                threadProductMapper.batchInsert(list);
                list.clear();
            }
        }
        if (list.size() > 0) {
            threadProductMapper.batchInsert(list);
            list.clear();
        }

    }

    public int getThreadSize() {
        return threadSize;
    }

    public void setThreadSize(int threadSize) {
        this.threadSize = threadSize;
    }

    public ThreadProductMapper getThreadProductMapper() {
        return threadProductMapper;
    }

    public void setThreadProductMapper(ThreadProductMapper threadProductMapper) {
        this.threadProductMapper = threadProductMapper;
    }
}
